package com.example.xxcjgl.ui.components.score

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material.icons.filled.LockOpen
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp

@Composable
fun EncryptionToggle(
    isEncrypted: Boolean,
    onEncryptionChange: (Boolean) -> Unit,
    modifier: Modifier = Modifier
) {
    Card(
        modifier = modifier.fillMaxWidth(),
        colors = CardDefaults.cardColors(
            containerColor = if (isEncrypted) Color(0xFFFFF8E1) else Color(0xFFE8F5E8)
        )
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Icon(
                imageVector = if (isEncrypted) Icons.Filled.Lock else Icons.Filled.LockOpen,
                contentDescription = null,
                tint = if (isEncrypted) Color(0xFFFFA000) else Color(0xFF388E3C)
            )

            Spacer(modifier = Modifier.width(16.dp))

            Column(modifier = Modifier.weight(1f)) {
                Text(
                    text = if (isEncrypted) "成绩已加密" else "成绩未加密",
                    fontWeight = FontWeight.Medium
                )
                Text(
                    text = if (isEncrypted) "需要密钥才能查看" else "所有人可见",
                    style = MaterialTheme.typography.bodySmall,
                    color = Color.Gray
                )
            }

            Switch(
                checked = isEncrypted,
                onCheckedChange = onEncryptionChange
            )
        }
    }
}

@Composable
fun EncryptionKeyInput(
    key: String,
    onKeyChange: (String) -> Unit,
    modifier: Modifier = Modifier
) {
    Column(modifier = modifier) {
        OutlinedTextField(
            value = key,
            onValueChange = onKeyChange,
            label = { Text("加密密钥") },
            modifier = Modifier.fillMaxWidth(),
            placeholder = { Text("请输入加密密钥") },
            visualTransformation = PasswordVisualTransformation(),
            singleLine = true
        )

        Text(
            text = "请妥善保管密钥，丢失后将无法查看加密成绩",
            style = MaterialTheme.typography.bodySmall,
            color = Color.Gray,
            modifier = Modifier.padding(top = 4.dp, start = 4.dp)
        )
    }
}